package com.mouse.hadoop.explorer.controller;


import com.mouse.hadoop.explorer.jobs.WordCountTask;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.mapreduce.Job;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * @program: hadoop-samples
 * @description:
 * @author: Chenjiabin
 * @create: 2019/5/22 17:48
 **/
@RestController
@RequestMapping("job")
public class JobController {
    @RequestMapping("test")
    public String test() throws IOException, ClassNotFoundException, InterruptedException {
        Job job = Job.getInstance();
        job.setJobName("xxxx");
        /***************************
         *......
         *在这里，和普通的MapReduce一样，设置各种需要的东西
         *......
         ***************************/

        //下面为了远程提交添加设置：
        Configuration conf = job.getConfiguration();
        conf.set("fs.default.name", "hdfs://10.28.0.62:9000");
        conf.set("mapreduce.app-submission.cross-platform", "true");//意思是跨平台提交，在windows下如果没有这句代码会报错 "/bin/bash: line 0: fg: no job control"，去网上搜答案很多都说是linux和windows环境不同导致的一般都是修改YarnRunner.java，但是其实添加了这行代码就可以了。
        conf.set("mapreduce.framework.name", "yarn");
        conf.set("yarn.application.classpath",
                "/usr/local/hadoop/etc/hadoop," +
                        "/usr/local/hadoop/share/hadoop/common/*," +
                        "/usr/local/hadoop/share/hadoop/common/lib/*," +
                        "/usr/local/hadoop/share/hadoop/hdfs/*," +
                        "/usr/local/hadoop/share/hadoop/hdfs/lib/*," +
                        "/usr/local/hadoop/share/hadoop/mapreduce/*," +
                        "/usr/local/hadoop/share/hadoop/mapreduce/lib/*," +
                        "/usr/local/hadoop/share/hadoop/yarn/*, " +
                        "/usr/local/hadoop/share/hadoop/yarn/lib/*");
        conf.set("mapred.child.java.opts", "-Xmx1024m");

        job.setJarByClass(WordCountTask.class);

        job.submit();
        return job.getJobID().toString();
    }
}
